package main

import (
	"fmt"
	"os"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/config"
	"github.com/tidwall/gjson"

	astilectron "github.com/asticode/go-astilectron"
	bootstrap "github.com/asticode/go-astilectron-bootstrap"
)

var w *astilectron.Window
var IsDebug = true
var AppName = "oldfeel"

// golang electron 客户端
func main() {
	if !IsDebug {
		start("index.html")
	} else {
		start("http://127.0.0.1:4444")
	}
}

func start(homepage string) {
	// Run bootstrap
	if err := bootstrap.Run(bootstrap.Options{
		Asset: Asset,
		AstilectronOptions: astilectron.Options{
			AppName:            AppName,
			AppIconDarwinPath:  "resources/icon.icns",
			AppIconDefaultPath: "resources/icon.png",
		},
		Debug:    IsDebug,
		Homepage: homepage,
		MenuOptions: []*astilectron.MenuItemOptions{{
			Label: astilectron.PtrStr("文件"),
			SubMenu: []*astilectron.MenuItemOptions{
				{
					Label: astilectron.PtrStr("关于"),
					OnClick: func(e astilectron.Event) (deleteListener bool) {
						bootstrap.SendMessage(w, "关于", "", func(m *bootstrap.MessageIn) {})
						return
					},
				},
				{
					Label: astilectron.PtrStr("服务器配置"),
					OnClick: func(e astilectron.Event) (deleteListener bool) {
						bootstrap.SendMessage(w, "服务器配置", "", func(m *bootstrap.MessageIn) {})
						return
					},
				},
				{Role: astilectron.MenuItemRoleClose},
			},
		}, {
			Label: astilectron.PtrStr("首页"),
			OnClick: func(e astilectron.Event) (deleteListener bool) {
				bootstrap.SendMessage(w, "首页", "", func(m *bootstrap.MessageIn) {})
				return
			},
		}},
		OnWait: func(_ *astilectron.Astilectron, iw *astilectron.Window, _ *astilectron.Menu, _ *astilectron.Tray, _ *astilectron.Menu) error {
			w = iw
			w.OpenDevTools()
			w.Maximize()
			return nil
		},
		MessageHandler: handleMessages,
		RestoreAssets:  RestoreAssets,
		WindowOptions: &astilectron.WindowOptions{
			BackgroundColor: astilectron.PtrStr("#333"),
			Center:          astilectron.PtrBool(true),
			Height:          astilectron.PtrInt(700),
			Width:           astilectron.PtrInt(700),
		},
	}); err != nil {
		fmt.Println(err)
	}
}

func handleMessages(_ *astilectron.Window, m bootstrap.MessageIn) (payload interface{}, err error) {
	beego.Info("handleMessages", m.Name, m.Payload)
	switch m.Name {
	case "注册":
		payload = "开始注册"
		go client_reg(0)
	case "获取用户列表":
		fmt.Println("获取用户列表")
		payload = "用户列表"
	case "发送文字":
		payload = "发送成功"
		targetId := gjson.GetBytes(m.Payload, "targetId").Int()
		content := gjson.GetBytes(m.Payload, "content").String()
		sendMessage(targetId, content)
	case "获取服务器配置":
		filename := "client.conf"
		var conf config.Configer
		conf, err = config.NewConfig("ini", filename)
		if err != nil {
			fmt.Println("conf err", err)
			os.Create(filename)
		}
		服务器地址 := conf.String("服务器地址")
		服务器UDP地址 := conf.String("服务器UDP地址")
		服务器接口地址 := conf.String("服务器接口地址")
		本地端口 := conf.String("本地端口")
		payload = map[string]interface{}{
			"服务器地址":    服务器地址,
			"服务器UDP地址": 服务器UDP地址,
			"服务器接口地址":  服务器接口地址,
			"本地端口":     本地端口}
	case "服务器配置":
		filename := "client.conf"
		var conf config.Configer
		conf, err = config.NewConfig("ini", filename)
		if err != nil {
			fmt.Println("conf err", err)
			os.Create(filename)
		}
		conf.Set("服务器地址", gjson.GetBytes(m.Payload, "服务器地址").String())
		conf.Set("服务器UDP地址", gjson.GetBytes(m.Payload, "服务器UDP地址").String())
		conf.Set("服务器接口地址", gjson.GetBytes(m.Payload, "服务器接口地址").String())
		conf.Set("本地端口", gjson.GetBytes(m.Payload, "本地端口").String())
		conf.SaveConfigFile(filename)
	}
	return
}

func sendMessage(targetId int64, content string) {

}
